1. Bloque de inicializacion de librerias

ejecutar el siguiente código si no se dispone del paquete ggmap: devtools::install_github(“dkahle/ggmap”)

ejecutar el siguiente código si al ejecutar los comandos aparecen errores relacionados con el environment: devtools::install_github("hadley/ggplot2@v2.2.0")

library(ggmap)
## Loading required package: ggplot2
## Google Maps API Terms of Service: http://developers.google.com/maps/terms.
## Please cite ggmap if you use it: see citation("ggmap") for details.

2. Bloque de parametros iniciales

setwd("~/Desktop/Taller")

3. Bloque de carga de informacion

Viviendas <- read.csv("listings.csv")

4. Bloque de visualizacion de mapas

Hay tres formas de seleccionar la zona a visualizar

  1. A partir de las coordenadas (longitud y la latitud)
Madrid=c(-3.6883432,40.453054)
map.Madrid <- get_map(location = Madrid)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.453054,-3.688343&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN
ggmap(map.Madrid)

  1. A partir de una posicion concreta
Barcelona <- geocode("Barcelona",source = "google")
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Barcelona
map.Barcelona <- get_map(location = Barcelona)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=41.385064,2.173404&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN
ggmap(map.Barcelona)

  1. A partir de la indicación de la zona perimetral
PeninsulaIberica <- c(left=-12,bottom=34,right=4,top=44.5)
map.PeninsulaIberica <- get_map(PeninsulaIberica)
## Warning: bounding box given to google - spatial extent only approximate.
## converting bounding box to center/zoom specification. (experimental)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=39.25,-4&zoom=6&size=640x640&scale=2&maptype=terrain&language=en-EN
ggmap(map.PeninsulaIberica)

EJERCICIO: Visualizar un mapa de Nueva York

NuevaYork <- geocode("Nueva York",source = "google")
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Nueva%20York
map.NuevaYork <- get_map(location = NuevaYork)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.712784,-74.005941&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN
ggmap(map.NuevaYork)

5. Bloque de tipos de mapas

el maptype por defecto es “terrain”

map.PeninsulaIberica1 <- get_map(PeninsulaIberica, maptype = "satellite")
## Warning: bounding box given to google - spatial extent only approximate.
## converting bounding box to center/zoom specification. (experimental)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=39.25,-4&zoom=6&size=640x640&scale=2&maptype=satellite&language=en-EN
ggmap(map.PeninsulaIberica1)

map.PeninsulaIberica2 <- get_map(PeninsulaIberica, maptype = "hybrid")
## Warning: bounding box given to google - spatial extent only approximate.
## converting bounding box to center/zoom specification. (experimental)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=39.25,-4&zoom=6&size=640x640&scale=2&maptype=hybrid&language=en-EN
ggmap(map.PeninsulaIberica2)

map.PeninsulaIberica3 <- get_map(PeninsulaIberica, maptype = "roadmap")
## Warning: bounding box given to google - spatial extent only approximate.
## converting bounding box to center/zoom specification. (experimental)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=39.25,-4&zoom=6&size=640x640&scale=2&maptype=roadmap&language=en-EN
ggmap(map.PeninsulaIberica3)

map.PeninsulaIberica4 <- get_map(PeninsulaIberica, maptype = "watercolor")
## maptype = "watercolor" is only available with source = "stamen".
## resetting to source = "stamen"...
## Source : http://tile.stamen.com/watercolor/6/29/23.jpg
## Source : http://tile.stamen.com/watercolor/6/30/23.jpg
## Source : http://tile.stamen.com/watercolor/6/31/23.jpg
## Source : http://tile.stamen.com/watercolor/6/32/23.jpg
## Source : http://tile.stamen.com/watercolor/6/29/24.jpg
## Source : http://tile.stamen.com/watercolor/6/30/24.jpg
## Source : http://tile.stamen.com/watercolor/6/31/24.jpg
## Source : http://tile.stamen.com/watercolor/6/32/24.jpg
## Source : http://tile.stamen.com/watercolor/6/29/25.jpg
## Source : http://tile.stamen.com/watercolor/6/30/25.jpg
## Source : http://tile.stamen.com/watercolor/6/31/25.jpg
## Source : http://tile.stamen.com/watercolor/6/32/25.jpg
ggmap(map.PeninsulaIberica)

EJERCICIO: Visualizar un mapa de Nueva York tipo satellite

map.NuevaYork <- get_map(NuevaYork, maptype = "satellite")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.712784,-74.005941&zoom=10&size=640x640&scale=2&maptype=satellite&language=en-EN
ggmap(map.NuevaYork)

6. Bloque de zoom

Bernabeu=c(-3.6883432,40.453054)
map.Bernabeu <- get_map(location = Bernabeu,zoom = 17,maptype = "satellite")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.453054,-3.688343&zoom=17&size=640x640&scale=2&maptype=satellite&language=en-EN
ggmap(map.Bernabeu)

dev.off()
## null device 
##           1
png("./mapa bernabeu.png")
ggmap(map.Bernabeu)
dev.off()
## null device 
##           1
ArtedeMedir <- geocode("El Arte de Medir, Calle Cundinamarca, Madrid",source = "google")
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=El%20Arte%20de%20Medir%2C%20Calle%20Cundinamarca%2C%20Madrid
map.ArtedeMedir <- get_map(location = as.numeric(ArtedeMedir),zoom = 20,maptype = "satellite")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.470838,-3.631555&zoom=20&size=640x640&scale=2&maptype=satellite&language=en-EN
ggmap(map.ArtedeMedir)

dev.off()
## null device 
##           1
png("./mapa elartedemedir.png")
ggmap(map.ArtedeMedir)
dev.off()
## null device 
##           1

EJERCICIO: Visualizar un mapa de Nueva York tipo roadmap con Zoom 14. Además guardarlo en un fichero

NuevaYork <- geocode("NuevaYork",source = "google")
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=NuevaYork
map.NuevaYork<- get_map(location = as.numeric(ArtedeMedir),zoom = 14,maptype = "roadmap")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.470838,-3.631555&zoom=14&size=640x640&scale=2&maptype=roadmap&language=en-EN
ggmap(map.NuevaYork)

7. Bloque de distancias

desde=c("El Arte de Medir, Calle Cundinamarca, Madrid")
hasta=c("Medialab-Prado, Calle de la Alameda, Madrid")

mapdist(desde, hasta, mode = "driving")
## Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=El%20Arte%20de%20Medir%2C%20Calle%20Cundinamarca%2C%20Madrid&destinations=Medialab-Prado%2C%20Calle%20de%20la%20Alameda%2C%20Madrid&mode=driving&language=en-EN
##                                           from
## 1 El Arte de Medir, Calle Cundinamarca, Madrid
##                                            to     m     km    miles
## 1 Medialab-Prado, Calle de la Alameda, Madrid 17658 17.658 10.97268
##   seconds minutes     hours
## 1    1227   20.45 0.3408333
mapdist(desde, hasta, mode = "bicycling")
## Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=El%20Arte%20de%20Medir%2C%20Calle%20Cundinamarca%2C%20Madrid&destinations=Medialab-Prado%2C%20Calle%20de%20la%20Alameda%2C%20Madrid&mode=bicycling&language=en-EN
##                                           from
## 1 El Arte de Medir, Calle Cundinamarca, Madrid
##                                            to     m     km    miles
## 1 Medialab-Prado, Calle de la Alameda, Madrid 11732 11.732 7.290265
##   seconds  minutes     hours
## 1    2596 43.26667 0.7211111
mapdist(desde, hasta, mode = "walking")
## Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=El%20Arte%20de%20Medir%2C%20Calle%20Cundinamarca%2C%20Madrid&destinations=Medialab-Prado%2C%20Calle%20de%20la%20Alameda%2C%20Madrid&mode=walking&language=en-EN
##                                           from
## 1 El Arte de Medir, Calle Cundinamarca, Madrid
##                                            to     m     km    miles
## 1 Medialab-Prado, Calle de la Alameda, Madrid 10195 10.195 6.335173
##   seconds minutes    hours
## 1    7707  128.45 2.140833

EJERCICIO: Calcular la distancia entre Atocha y Chamartin en bicicleta, caminando y en coche

desde=c("Estación de Madrid Atocha, Plaza Emperador Carlos V, Madrid")
hasta=c("Chamartín, Madrid")

mapdist(desde, hasta, mode = "driving")
## Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=Estaci%C3%B3n%20de%20Madrid%20Atocha%2C%20Plaza%20Emperador%20Carlos%20V%2C%20Madrid&destinations=Chamart%C3%ADn%2C%20Madrid&mode=driving&language=en-EN
##                                                          from
## 1 Estación de Madrid Atocha, Plaza Emperador Carlos V, Madrid
##                  to     m     km    miles seconds minutes     hours
## 1 Chamartín, Madrid 11356 11.356 7.056618    1077   17.95 0.2991667
mapdist(desde, hasta, mode = "bicycling")
## Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=Estaci%C3%B3n%20de%20Madrid%20Atocha%2C%20Plaza%20Emperador%20Carlos%20V%2C%20Madrid&destinations=Chamart%C3%ADn%2C%20Madrid&mode=bicycling&language=en-EN
##                                                          from
## 1 Estación de Madrid Atocha, Plaza Emperador Carlos V, Madrid
##                  to    m    km    miles seconds minutes     hours
## 1 Chamartín, Madrid 6888 6.888 4.280203    1590    26.5 0.4416667
mapdist(desde, hasta, mode = "walking")
## Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=Estaci%C3%B3n%20de%20Madrid%20Atocha%2C%20Plaza%20Emperador%20Carlos%20V%2C%20Madrid&destinations=Chamart%C3%ADn%2C%20Madrid&mode=walking&language=en-EN
##                                                          from
## 1 Estación de Madrid Atocha, Plaza Emperador Carlos V, Madrid
##                  to    m    km    miles seconds  minutes    hours
## 1 Chamartín, Madrid 6362 6.362 3.953347    5077 84.61667 1.410278
  1. Bloque de posicionamiento de objetos
#ArtedeMedir <- geocode("El Arte de Medir, Calle Cundinamarca, Madrid",source = "google")
#map.ArtedeMedir <- get_map(location = as.numeric(ArtedeMedir),zoom = 18,maptype = "roadmap")
str(ArtedeMedir)
## 'data.frame':    1 obs. of  2 variables:
##  $ lon: num -3.63
##  $ lat: num 40.5
ggmap(map.ArtedeMedir) + geom_point(aes(x = lon, y = lat),
                                    data = ArtedeMedir, colour = 'red',
                                    size = 4)

str(Viviendas)
## 'data.frame':    7446 obs. of  16 variables:
##  $ id                            : int  7328003 6289024 3778195 7056183 6847125 3453225 336869 5876946 316712 6584180 ...
##  $ name                          : Factor w/ 7251 levels "  Habitacion silenciosa ",..: 1779 5377 6958 5412 4151 4291 2858 5912 50 3977 ...
##  $ host_id                       : int  9205789 3305388 10940437 19558259 21039137 1528801 1713524 16859206 162701 34212673 ...
##  $ host_name                     : Factor w/ 1821 levels "","(email hidden)",..: 64 504 64 1001 349 500 1378 1718 1304 1208 ...
##  $ neighbourhood_group           : Factor w/ 21 levels "Arganzuela","Barajas",..: 4 4 4 4 4 4 4 4 4 4 ...
##  $ neighbourhood                 : Factor w/ 126 levels "Abrantes","Acacias",..: 83 83 83 83 83 83 83 83 83 83 ...
##  $ latitude                      : num  40.4 40.4 40.4 40.4 40.4 ...
##  $ longitude                     : num  -3.71 -3.72 -3.71 -3.71 -3.71 ...
##  $ room_type                     : Factor w/ 3 levels "Entire home/apt",..: 1 2 3 2 2 2 2 2 2 2 ...
##  $ price                         : int  60 25 22 45 37 19 55 95 25 25 ...
##  $ minimum_nights                : int  1 1 1 1 2 3 2 1 2 1 ...
##  $ number_of_reviews             : int  3 1 12 0 3 0 75 2 60 3 ...
##  $ last_review                   : Factor w/ 420 levels "","2011-11-26",..: 409 350 401 1 338 1 415 332 406 373 ...
##  $ reviews_per_month             : num  3 0.42 0.91 NA 1 NA 1.81 0.44 1.38 0.77 ...
##  $ calculated_host_listings_count: int  2 1 11 1 1 12 2 6 1 5 ...
##  $ availability_365              : int  343 330 347 335 0 26 318 302 327 295 ...
#Madrid=c(-3.6883432,40.453054)
map.Madrid <- get_map(location = Madrid)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.453054,-3.688343&zoom=10&size=640x640&scale=2&maptype=terrain&language=en-EN
ggmap(map.Madrid)+ geom_point(aes(x = longitude, y = latitude),
                                       data = Viviendas, colour = 'red', alpha=1)

9. Bloque de consulta de consumo de API

geocodeQueryCheck()
## 2496 geocoding queries remaining.

10. Bloque de representacion de variables categoricas

str(Viviendas)
## 'data.frame':    7446 obs. of  16 variables:
##  $ id                            : int  7328003 6289024 3778195 7056183 6847125 3453225 336869 5876946 316712 6584180 ...
##  $ name                          : Factor w/ 7251 levels "  Habitacion silenciosa ",..: 1779 5377 6958 5412 4151 4291 2858 5912 50 3977 ...
##  $ host_id                       : int  9205789 3305388 10940437 19558259 21039137 1528801 1713524 16859206 162701 34212673 ...
##  $ host_name                     : Factor w/ 1821 levels "","(email hidden)",..: 64 504 64 1001 349 500 1378 1718 1304 1208 ...
##  $ neighbourhood_group           : Factor w/ 21 levels "Arganzuela","Barajas",..: 4 4 4 4 4 4 4 4 4 4 ...
##  $ neighbourhood                 : Factor w/ 126 levels "Abrantes","Acacias",..: 83 83 83 83 83 83 83 83 83 83 ...
##  $ latitude                      : num  40.4 40.4 40.4 40.4 40.4 ...
##  $ longitude                     : num  -3.71 -3.72 -3.71 -3.71 -3.71 ...
##  $ room_type                     : Factor w/ 3 levels "Entire home/apt",..: 1 2 3 2 2 2 2 2 2 2 ...
##  $ price                         : int  60 25 22 45 37 19 55 95 25 25 ...
##  $ minimum_nights                : int  1 1 1 1 2 3 2 1 2 1 ...
##  $ number_of_reviews             : int  3 1 12 0 3 0 75 2 60 3 ...
##  $ last_review                   : Factor w/ 420 levels "","2011-11-26",..: 409 350 401 1 338 1 415 332 406 373 ...
##  $ reviews_per_month             : num  3 0.42 0.91 NA 1 NA 1.81 0.44 1.38 0.77 ...
##  $ calculated_host_listings_count: int  2 1 11 1 1 12 2 6 1 5 ...
##  $ availability_365              : int  343 330 347 335 0 26 318 302 327 295 ...
table(Viviendas$room_type)
## 
## Entire home/apt    Private room     Shared room 
##            4580            2760             106
#Madrid=c(-3.6883432,40.453054)
map.Madrid <- get_map(location = Madrid,zoom=12,maptype = "satellite")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.453054,-3.688343&zoom=12&size=640x640&scale=2&maptype=satellite&language=en-EN
ggmap(map.Madrid)+ geom_point(aes(x = longitude, y = latitude),
                              data = Viviendas, colour = 4+as.numeric(Viviendas$room_type))
## Warning: Removed 74 rows containing missing values (geom_point).

#Bernabeu=c(-3.7083759,40.4169335)
map.Bernabeu <- get_map(location = Bernabeu,zoom = 16,maptype = "satellite")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.453054,-3.688343&zoom=16&size=640x640&scale=2&maptype=satellite&language=en-EN
ggmap(map.Bernabeu) + geom_point(aes(x = longitude, y = latitude),
                                 data = Viviendas, colour = 4+as.numeric(Viviendas$room_type))
## Warning: Removed 7409 rows containing missing values (geom_point).

EJERCICIO: Representar en un mapa tipo satellite las viviendas cercanas a Sol con Zoom 18

Sol=c(-3.6883432,40.453054)
map.Sol <- get_map(location = Sol,zoom = 18,maptype = "satellite")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.453054,-3.688343&zoom=18&size=640x640&scale=2&maptype=satellite&language=en-EN
ggmap(map.Sol) + geom_point(aes(x = longitude, y = latitude),
                                 data = Viviendas, colour = 4+as.numeric(Viviendas$room_type))
## Warning: Removed 7445 rows containing missing values (geom_point).

11. Bloque de representacion de variables continuas

str(Viviendas)
## 'data.frame':    7446 obs. of  16 variables:
##  $ id                            : int  7328003 6289024 3778195 7056183 6847125 3453225 336869 5876946 316712 6584180 ...
##  $ name                          : Factor w/ 7251 levels "  Habitacion silenciosa ",..: 1779 5377 6958 5412 4151 4291 2858 5912 50 3977 ...
##  $ host_id                       : int  9205789 3305388 10940437 19558259 21039137 1528801 1713524 16859206 162701 34212673 ...
##  $ host_name                     : Factor w/ 1821 levels "","(email hidden)",..: 64 504 64 1001 349 500 1378 1718 1304 1208 ...
##  $ neighbourhood_group           : Factor w/ 21 levels "Arganzuela","Barajas",..: 4 4 4 4 4 4 4 4 4 4 ...
##  $ neighbourhood                 : Factor w/ 126 levels "Abrantes","Acacias",..: 83 83 83 83 83 83 83 83 83 83 ...
##  $ latitude                      : num  40.4 40.4 40.4 40.4 40.4 ...
##  $ longitude                     : num  -3.71 -3.72 -3.71 -3.71 -3.71 ...
##  $ room_type                     : Factor w/ 3 levels "Entire home/apt",..: 1 2 3 2 2 2 2 2 2 2 ...
##  $ price                         : int  60 25 22 45 37 19 55 95 25 25 ...
##  $ minimum_nights                : int  1 1 1 1 2 3 2 1 2 1 ...
##  $ number_of_reviews             : int  3 1 12 0 3 0 75 2 60 3 ...
##  $ last_review                   : Factor w/ 420 levels "","2011-11-26",..: 409 350 401 1 338 1 415 332 406 373 ...
##  $ reviews_per_month             : num  3 0.42 0.91 NA 1 NA 1.81 0.44 1.38 0.77 ...
##  $ calculated_host_listings_count: int  2 1 11 1 1 12 2 6 1 5 ...
##  $ availability_365              : int  343 330 347 335 0 26 318 302 327 295 ...
hist(Viviendas$price)

hist(log(Viviendas$price))

#Madrid=c(-3.6883432,40.453054)
map.Madrid <- get_map(location = Madrid,zoom=12,maptype = "satellite")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.453054,-3.688343&zoom=12&size=640x640&scale=2&maptype=satellite&language=en-EN
ggmap(map.Madrid)+ geom_point(aes(x = longitude, y = latitude,col = room_type,size=price) ,
                              data = Viviendas)
## Warning: Removed 74 rows containing missing values (geom_point).

#Bernabeu=c(-3.6883432,40.453054)
map.Bernabeu <- get_map(location = Bernabeu,zoom = 16,maptype = "satellite")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=40.453054,-3.688343&zoom=16&size=640x640&scale=2&maptype=satellite&language=en-EN
ggmap(map.Bernabeu) + geom_point(aes(x = longitude, y = latitude,col = room_type,size=price) ,
                                 data = Viviendas)
## Warning: Removed 7409 rows containing missing values (geom_point).

ggmap(map.Bernabeu) + geom_point(aes(x = longitude, y = latitude,col = room_type,size=price) ,
                                 data = Viviendas,shape=8)
## Warning: Removed 7409 rows containing missing values (geom_point).